package com.example.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.LikeDao;
import com.example.demo.pojo.CarGood;
import com.example.demo.pojo.Like;

@RestController
public class LikeController {
	@Autowired
	LikeDao likeDao;
    @GetMapping("getAllLike")
    public Map<String, List<Like>> getAllLike(String id) {
    	
		List<Like> all=likeDao.findByUserId(id);
		Map<String,List<Like>> map=new HashMap<String,List<Like>>();
		map.put("list", all);
		System.out.println(map.toString());
		return map;
    }
    @PostMapping("addLike")
    public Map addLike(@RequestBody Like like) {
    	System.out.println(like);
    	String result="no";
    	Map m=new HashMap();
    	try {
    		likeDao.save(like);
    		result="yes";
    	}catch(Exception e){
    		throw e;
    	}
    	m.put("result", result);
    	return m;
    }
    @GetMapping("deleteLike")
    public Map<String,String> deleteLike(String userId,String goodId){
    	Map<String,String> map =new HashMap<String, String>();
    	String result="no";
    	try {
    		likeDao.deleteByUserIdAndGoodId(userId, goodId);
    		result="yes";
    		
    	}catch(Exception e) {
    		throw e;
    	}
    	map.put("result", result);
    	return map;
    }
}
